package com.wzw.springboot.principle.use.security.infrastructure.handle;

import com.wzw.springboot.principle.mvc.handler.ServletHandler;
import com.wzw.springboot.principle.use.security.manage.TokenManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户登出成功后的处理逻辑
 *
 * @author Wangzhiwen
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class CustomizedLogoutSuccessHandler implements LogoutSuccessHandler {

    private final TokenManager tokenManager;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        // 清空token
        tokenManager.clean(request);
        ServletHandler.write(response, "退出成功");
    }
}
